﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;

using EHTSaleManager.Code;
using EHTSaleManager.Code.BLL;
using EHTSaleManager.Code.Utility;
using EHTSaleManager.Code.Entity;

namespace EHTSaleManager
{
    public partial class IndexRegister : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataSet dsRegion = RegionProvider.GetRegions();
                this.ddlRegion.DataValueField = "RegionID";
                this.ddlRegion.DataTextField = "RegionName";
                this.ddlRegion.DataSource = dsRegion;
                this.ddlRegion.DataBind();

                DataSet dsLevelServices = ServiceLevelsProvider.GetServiceLevels();
                this.ddlServiceLevel.DataValueField = "SeviceLevelID";
                this.ddlServiceLevel.DataTextField = "Description";
                ddlServiceLevel.DataSource = dsLevelServices;
                ddlServiceLevel.DataBind();
                ddlServiceLevel.Items.Insert(0, new ListItem("-- LevelService --", "-1"));
            }

        }
        private Boolean ValidateData()
        {
            String message = "";
            Boolean isSuccess = true;

            if (ddlRegion.SelectedValue == "-1")
            {
                message += "Velg Region <br />";
                isSuccess = false;
            }
            if (ddlServiceLevel.SelectedValue == "-1")
            {
                message += "Velg Service Level <br />";
                isSuccess = false;
            }
            if (this.txtPassword.Text.Trim().Length < 6)
            {
                message += "Passord må ha lengh >= 6";
                isSuccess = false;
            }

            if (!isSuccess)
                ltrMessage.Text = message;

            return isSuccess;
        }

        protected void ddlRegion_DataBound(object sender, EventArgs e)
        {
            this.ddlRegion.Items.Insert(0, new ListItem("velg region ...", "-1"));
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            if (!ValidateData())
                return;

            Page.Validate();

            if (Page.IsValid)
            {
                String userName = this.txtEmail.Text;
                String password = this.txtPassword.Text;
                String email = this.txtEmail.Text;
                MembershipUser theUser = Membership.GetUser(HttpContext.Current.User.Identity.Name);

                //trường hợp User Name chưa tồn tại mới cho đăng kí
                if (theUser == null)
                {
                    theUser = Membership.CreateUser(userName, password, email);// tạo user
                    Roles.AddUserToRole(userName, HangSo.ROLE_CUSTOMER); // thiết định vào nhóm Customer
                    //thiết định các thông tin cần thiết
                    ProfileBase theProfile = ProfileBase.Create(theUser.UserName);
                    theProfile.SetPropertyValue("FirstName", this.txtFirstName.Text.Trim());
                    theProfile.SetPropertyValue("LastName", this.txtLastName.Text.Trim());
                    theProfile.SetPropertyValue("Email", this.txtEmail.Text.Trim());
                    theProfile.SetPropertyValue("Phone", this.txtPhone.Text.Trim());
                    theProfile.SetPropertyValue("Fax", this.txtFax.Text.Trim());
                    theProfile.SetPropertyValue("City", this.txtCity.Text.Trim());
                    theProfile.SetPropertyValue("Address", this.txtAddress.Text.Trim());
                    theProfile.SetPropertyValue("RegionID", Int32.Parse(this.ddlRegion.SelectedValue));
                    theProfile.SetPropertyValue("PostCode", this.txtPostCode.Text.Trim());
                    theProfile.SetPropertyValue("SignatureUrl", "");
                    theProfile.SetPropertyValue("ToltalAccount", float.Parse("0"));

                    if (Int32.Parse(this.ddlServiceLevel.SelectedValue) == -1)
                    {
                        theProfile.SetPropertyValue("ServiceLevelID", 0);
                    }
                    else
                        theProfile.SetPropertyValue("ServiceLevelID", Int32.Parse(this.ddlServiceLevel.SelectedValue));

                    theProfile.Save();// lưu thông tin cần thiết

                    CurrentBalance theCurrentBalance = new CurrentBalance();
                    theCurrentBalance.CustomerID = theUser.ProviderUserKey.ToString();
                    int ID = Int32.Parse(theProfile.GetPropertyValue("ServiceLevelID").ToString());
                    theCurrentBalance.TotalAmount = float.Parse(ServiceLevelsProvider.GetServiceLevelByID(ID).ServiceCost.ToString());

                    CurrentBalanceProvider.ICurrentBalance(theCurrentBalance);
                    Response.Redirect("RegisterComfirmSuccess.aspx");
                }
                //tồn tại rồi thì hiển thị thông báo
                else
                {
                    ltrMessage.Text = "Denne E-post er alredy utganger";
                    this.txtEmail.Focus();
                }
            }
        }
    }
}